package com.crm.service.impl;

import com.crm.business.mapper.BusinessMapper;
import com.crm.common.core.domain.entity.SysUser;

import com.crm.common.utils.DateUtils;
import com.crm.domain.AssignRecord;
import com.crm.domain.RulePool;
import com.crm.domain.vo.TransferVo;
import com.crm.mapper.AssignRecordMapper;
import com.crm.mapper.ClueMapper;
import com.crm.service.IRulePoolService;
import com.crm.service.TransferService;
import com.crm.system.mapper.SysUserMapper;
import com.crm.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TansferServiceImpl implements TransferService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询所有转交记录
     * @param user
     * @return
     */
    @Override
    public List<TransferVo> selectTransferList(SysUser user) {
        List<TransferVo> transferList = new ArrayList<>();
        List<SysUser> userList = userService.selectUserList(user);
        for (SysUser sysUser : userList) {
            TransferVo transferVo = new TransferVo();
            // 通过用户
            int clueNum = clueMapper.countCluesByUser(sysUser.getUserName());
            int businessNum = businessMapper.countBusinessByUser(sysUser.getUserName());
            if (clueNum + businessNum > 0) {
                transferVo.setUserName(sysUser.getUserName());
                transferVo.setClueNum(clueNum);
                transferVo.setBusinessNum(businessNum);
                transferVo.setUserId(sysUser.getUserId());
                transferVo.setPhonenumber(sysUser.getPhonenumber());
                transferVo.setCreateTime(sysUser.getCreateTime());
                transferList.add(transferVo);
            }
        }
        return transferList;
    }
    /**
     * 处理转交
     * @param type
     * @param userId
     * @param transferUserId
     * @return
     */
    @Override
    public Map<String,Object> assignment(String type, Long userId, Long transferUserId) {
        String userName = sysUserMapper.selectUserById(userId).getUserName();
        String transferUserName = sysUserMapper.selectUserById(transferUserId).getUserName();
        if ("0".equals(type)){
            clueMapper.transfer(userName,transferUserName);
        }
        if ("1".equals(type)){
            businessMapper.transfer(userName,transferUserName);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code",200);
        result.put("msg","转交成功");
        return result;
    }
}


